/*
 * Dispatcher.cpp
 *
 *  Created on: 2012-5-18
 *      Author: WuyaMony
 */

#include "Dispatcher.h"

namespace my {

int add(int a, int b) {
	return a + b;
}
int mul(int a,int b){
	return a*b;
}

void Dispatcher::simulate(deque<int> &data,deque<int> &ABdata) {
	ArithmeticUnit adder(data, 4, add);
	ArithmeticUnit multiplier(data,3,mul);
	Clock::reset();
	Clock clock1(ABdata, multiplier);
	Clock clock2(data, adder);
	while(ABdata.size()>1 || multiplier.getTaskNum()>0){
		clock1.Tick();
		multiplier.Tick();
	}
	while(data.size()>1 || adder.getTaskNum()>0){
		clock2.Tick();
		adder.Tick();
	}
	cout << "time cost :" << Clock::getTimeCost() << endl;
}
void Dispatcher::mul_sim() {
	srand(time(0));
	deque<int> data,ABdata;
	for (int n = 4; n <= 15; ++n) {
		cout << "n=" << n << endl;
		data.clear();
		ABdata.clear();
		for (int j = 0; j < n; ++j) {
			ABdata.push_back(rand());
			cout << "add " << ABdata.back() << endl;
			ABdata.push_back(rand());
			cout << "add " << ABdata.back() << endl;
		}
		simulate(data,ABdata);
	}
}
}

/* namespace my */
